﻿using SpiderStudy.Utils.Currency;
using System;

namespace SpiderStudy.Utils
{
    internal class TimeHelper
    {
        /// <summary>
        /// 计算每日的时间差 返回相差多少天  用endtime-satartTime 若返回负值则代表出现错误
        /// </summary>
        /// <param name="StartTime">起始时间</param>
        /// <param name="EndTime">结束时间</param>
        /// <returns></returns>
        public int DisparityDay(string StartTime, string EndTime)
        {
            try
            {
                TimeSpan span = TimeSubtract(StartTime, EndTime);
                return span.Days;
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return -1;
        }

        /// <summary>
        /// 计算小时的时间差 用endtime-satartTime 若返回负值则代表出现错误
        /// </summary>
        /// <param name="StartTime">起始时间</param>
        /// <param name="EndTime">结束时间</param>
        /// <returns></returns>
        public int DisparityHours(string StartTime, string EndTime)
        {
            try
            {
                TimeSpan span = TimeSubtract(StartTime, EndTime);
                return span.Hours;
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return -1;
        }

        /// <summary>
        /// 计算时间的差，返回TimeSpan
        /// </summary>
        /// <param name="StartTime"></param>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        public TimeSpan TimeSubtract(string StartTime, string EndTime)
        {
            TimeSpan span = new TimeSpan();
            try { span = Convert.ToDateTime(EndTime).Subtract(Convert.ToDateTime(StartTime)); }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return span;
        }
    }
}